<?php

namespace app\admin\controller;

use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 机器人产品
 *
 * admin-RobotProducts
 *
 * @Apidoc\Title("机器人产品")
 * @Apidoc\Group("api")
 */
class RobotProducts extends BaseController
{
    /**
     * 新增
     *
     * admin-RobotProducts-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\RobotProducts::create($data['robot_products']);
            return success();
        } else {
            $options = [];
            $robot_products__is_open_api_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['robot_products__is_open_api_options'] = $robot_products__is_open_api_option;
            $robot_products__is_remove_brand_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['robot_products__is_remove_brand_options'] = $robot_products__is_remove_brand_option;
            $robot_products__is_open_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['robot_products__is_open_options'] = $robot_products__is_open_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-RobotProducts-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\RobotProducts::update($data['robot_products']);
            return success();
        } else {
            $data = \app\common\model\RobotProducts::find(input('robot_products__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['robot_products__' . $key] = $val;
            }
            View::assign('r', $r);
            $options = [];
            $robot_products__is_open_api_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['robot_products__is_open_api_options'] = $robot_products__is_open_api_option;
            $robot_products__is_remove_brand_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['robot_products__is_remove_brand_options'] = $robot_products__is_remove_brand_option;
            $robot_products__is_open_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
            $options['robot_products__is_open_options'] = $robot_products__is_open_option;
            View::assign($options);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-RobotProducts-delete
     */
    public function delete()
    {
        \app\common\model\RobotProducts::destroy(input('robot_products__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-RobotProducts-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_robot_products');
        if (input('robot_products__name', '') !== '') {
            $query->where('db_robot_products.name', 'like', '%' . input('robot_products__name', '', 'search_in') . '%');
        }
        if (input('robot_products__is_open', '') !== '') {
            $query->where('db_robot_products.is_open', '=', input('robot_products__is_open', '', 'search_in'));
        }
        $query->where('db_robot_products.delete_time', '=', null);
        $query->field(['db_robot_products.id' => 'robot_products__id']);
        $query->field(['db_robot_products.name' => 'robot_products__name']);
        $query->field(['db_robot_products.word_limit' => 'robot_products__word_limit']);
        $query->field(['db_robot_products.daily_conversation_limit' => 'robot_products__daily_conversation_limit']);
        $query->field(['db_robot_products.is_open_api' => 'robot_products__is_open_api']);
        $query->field(['db_robot_products.is_remove_brand' => 'robot_products__is_remove_brand']);
        $query->field(['db_robot_products.duration_days' => 'robot_products__duration_days']);
        $query->field(['db_robot_products.price' => 'robot_products__price']);
        $query->field(['db_robot_products.sort' => 'robot_products__sort']);
        $query->field(['db_robot_products.is_open' => 'robot_products__is_open']);
        $query->order('db_robot_products.id', 'desc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        View::assign($data);
        $options = [];
        $robot_products__is_open_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
        $options['robot_products__is_open_options'] = $robot_products__is_open_option;
        $robot_products__id_option = \app\common\model\RobotProducts::column('name as name, id as id');
        $options['robot_products__id_options'] = $robot_products__id_option;
        $robot_products__is_open_api_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
        $options['robot_products__is_open_api_options'] = $robot_products__is_open_api_option;
        $robot_products__is_remove_brand_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
        $options['robot_products__is_remove_brand_options'] = $robot_products__is_remove_brand_option;
        $robot_products__is_open_option = [['name' => '是', 'id' => '1'], ['name' => '否', 'id' => '0']];
        $options['robot_products__is_open_options'] = $robot_products__is_open_option;
        View::assign($options);
        return View::fetch();
    }
}